<template>
  <!--验货信息-->
  <div>
    <el-dialog
      :visible.sync="open"
      title="验货信息"
      width="800px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="100px"
        size="mini"
      >
      <el-form-item label="商品分类" prop="typeId">
          <el-select v-model="queryParams.typeId" placeholder="请选择商品分类" clearable size="small">
              <el-option
                v-for="item in productTypeList"
                :key="item.id"
                :label="item.typeName"
                :value="item.id"
              />
            </el-select>
    </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          clearable
          class="filter-item"
          style="width: 200px"
          placeholder="请输入商品名称"
        />
      </el-form-item>
      <el-form-item label="采购批次号" prop="purchaseNumber">
        <el-input
          v-model="queryParams.purchaseNumber"
          clearable
          class="filter-item"
          style="width: 200px"
          placeholder="请输入采购批次号"
        />
      </el-form-item>
      <el-form-item label="商品条码" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          clearable
          class="filter-item"
          style="width: 200px"
          placeholder="请输入商品条码"
        />
      </el-form-item>
      <el-form-item label="验货日期">
        <el-date-picker
          v-model="daterangeCheckDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >查找</el-button
        ><el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
      </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="productList"
        ref="productListDetail"
        element-loading-text="正在查询中。。。"
        border
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
        @row-dblclick="dblclickSpecificationAdd"
      >
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="采购批次号" align="center" prop="purchaseNumber" min-width="165px" fixed/>
      <el-table-column label="商品条码" align="center" prop="barcode" min-width="155px" fixed/>
      <el-table-column label="商品编号" align="center" prop="productSn" min-width="120px" fixed/>
      <el-table-column label="商品分类" align="center" prop="typeName" min-width="120px"/>
      <el-table-column label="商品名称" align="center" prop="productName" min-width="120px"/>
      <el-table-column label="规格" align="center" prop="itemName" min-width="120px"/>
      <el-table-column label="计量单位" prop="measurement" min-width="80px" :formatter="measurementFormat" />
      <el-table-column label="验货日期" align="center" prop="checkDate" width="160px" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前库存数量" align="center" prop="storeCount" width="120px"/>
      <el-table-column label="验货数量" align="center" prop="qty" />
    
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" @click="handleSpecificationAdd">确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckProductdetail } from "@/api/supplier/checkProductdetail";
import { listProductType} from "@/api/supplier/productType";
export default {
  name: "Product",
  props: {
    productProps: {
      open: false,
      productList: [],
      
    },
    handleSelect: {
      type: Function
    },
    radiobox: true
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: [],
      },
      open: this.productProps.open,
      // 总条数
      total: 0,
      // 遮罩层
      loading: true,
      // 选中的商品
      checkedProduct: [],
      // 验货日期时间范围
      daterangeCheckDate: [],
      // 计量单位字典
      measurementOptions: [],
      // 选中数组
      ids: [],
     // 验货表格数据
      productList: [],
      // 商品分类表格数据
      productTypeList: [],
      // 保质期限年月日字典
      lifeTypeOptions: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("bus_measurement").then(response => {
      this.measurementOptions = response.data;
    });
    listProductType().then(response => {
        this.productTypeList = response.rows;
    });
  },
  methods: {
     /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询验货列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCheckDate && '' != this.daterangeCheckDate) {
        this.queryParams.params["beginCheckDate"] = this.daterangeCheckDate[0];
        this.queryParams.params["endCheckDate"] = this.daterangeCheckDate[1];
      }
      listCheckProductdetail(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 计量单位字典翻译
    measurementFormat(row, column) {
      return this.selectDictLabel(this.measurementOptions, row.measurement);
    },
   
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      
      if(!this.radiobox){
        this.checkedProduct = selection;
      }else{
        if(selection.length > 1){
          this.$refs.productListDetail.clearSelection()
          this.$refs.productListDetail.toggleRowSelection(selection.pop())
        }else{
          this.checkedProduct = selection;
        }  
      }
      
    },
    //在所有商品中选中对应商品进入订单列表的商品中
    handleSpecificationAdd() {
      this.checkedProduct.forEach((item) => {
        for (let i = 0; i < this.productList.length; i++) {
          if (this.productList[i].checkId === item.checkId) {

            this.productList.splice(i, 1);
            this.queryParams.ids.push(item.checkId);
            break;
          }
        }
      });
     
      this.handleSelect(this.checkedProduct)
      this.checkedProduct = [];

      this.open = false;
    },
    dblclickSpecificationAdd(row) {
      this.checkedProduct.push(row);
      this.handleSelect(this.checkedProduct);
      this.checkedProduct = [];

      this.open = false;
    },
    
  },
};
</script>

<style>
</style>